/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

import java.util.ArrayList;

/**
 *
 * @author Reinhard
 */
public class Response {
  public String failure_reason;
  public String warning_message;
  public int interval_value;
  public int complete_value;
  public int incomplete_value;
  public ArrayList<PeerInfo> peers=new ArrayList<PeerInfo>();
  
  public Response(String failure_reason, String warning_message, int interval_value, 
          int complete_value, int incomplete_value, ArrayList<PeerInfo> peers) {
    this.failure_reason = failure_reason;
    this.warning_message = warning_message;
    this.interval_value = interval_value;
    this.complete_value = complete_value;
    this.incomplete_value = incomplete_value;
    this.peers.addAll(peers);
  }

  public Response(String failure_reason, String warning_message, int interval_value,
          int complete_value, int incomplete_value, String peers) {
    this.failure_reason = failure_reason;
    this.warning_message = warning_message;
    this.interval_value = interval_value;
    this.complete_value = complete_value;
    this.incomplete_value = incomplete_value;

      String splitString[]=peers.split(" ");
      this.peers.clear();
      for(int i=0;i<splitString.length;i++)
      {
          this.peers.add(new PeerInfo(splitString[i]));
      }
  }
  
  public String getTrackerRespone() {
    StringBuilder sb = new StringBuilder();
    sb.append("failure_reason " + failure_reason);
    sb.append("\n");
    sb.append("warning_message " + warning_message);
    sb.append("\n");
    sb.append("interval " + Integer.toString(interval_value));
    sb.append("\n");
    sb.append("complete " + Integer.toString(complete_value));
    sb.append("\n");
    sb.append("incomplete " + Integer.toString(incomplete_value));
    sb.append("\n");
    sb.append("peers");
    for(int i=0;i<peers.size();i++)
    {
        sb.append(" "+peers.get(i).GetPeers());
    }
    sb.append("\n");
    return sb.toString();
  }

  public void setTrackerResponse(String input)
  {
      input=input.substring(15);
      failure_reason=input.substring(0, input.indexOf("\n"));
      input=input.substring(2);

      input=input.substring(16);
      warning_message=input.substring(0, input.indexOf("\n"));
      input=input.substring(2);

      input=input.substring(9);
      interval_value=Integer.valueOf(input.substring(0, input.indexOf("\n")));
      input=input.substring(2);

      input=input.substring(9);
      complete_value=Integer.valueOf(input.substring(0, input.indexOf("\n")));
      input=input.substring(2);

      input=input.substring(11);
      incomplete_value=Integer.valueOf(input.substring(0, input.indexOf("\n")));
      input=input.substring(2);

      String splitString[]=input.split(" ");
      peers.clear();
      for(int i=0;i<splitString.length;i++)
      {
          peers.add(new PeerInfo(splitString[i]));
      }
  }
  
}
